package com.lyf.juc.learn.sggadvance.future;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author LYF
 * @date 2022/7/1
 */
public class FutureThreadPoolTests {
    /*
    1.get()阻塞问题
    2.isDone轮询耗时问题
     */
    @Test
    public void test0() throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        FutureTask<String> futureTask1 = new FutureTask<String>(()->{
            try{TimeUnit.SECONDS.sleep(2); }catch (Exception e){
                e.printStackTrace();
            }
            return "task1 over";
        });

        FutureTask<String> futureTask2 = new FutureTask<String>(()->{
            try{TimeUnit.SECONDS.sleep(3); }catch (Exception e){
                e.printStackTrace();
            }
            return "task1 over";
        });

        executorService.submit(futureTask1);
        executorService.submit(futureTask2);

//        System.out.println("Main work..");
//        System.out.println(futureTask1.get());

//        System.out.println(futureTask1.get());//阻塞
//        System.out.println("Main work..");

//        System.out.println(futureTask1.get(2,TimeUnit.SECONDS));//阻塞
//        System.out.println("Main work..");

//        while (true){
//            if(futureTask1.isDone()){
//                System.out.println(futureTask1.get());
//                break;
//            }else {
//                System.out.println("还未处理完..");
//                TimeUnit.SECONDS.sleep(1);
//            }
//        }

    }
}
